package org.springframework.samples.petclinic.lift

import javax.servlet.ServletContext

import org.springframework.context.MessageSource
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.support.WebApplicationContextUtils

/**
 * It doesn't seem to be possible to use dependency injection for Lift snippets.
 * These functions work around this problem by looking up the beans by name
 * through the WebApplicationContext.
 */
object Utils {
  
  def getWebApplicationContext(sc : ServletContext): WebApplicationContext = {
    WebApplicationContextUtils.getWebApplicationContext(sc) 
  }
  
  def getClinic(servletContext : ServletContext): Clinic = {
    getWebApplicationContext(servletContext).getBean("clinic").asInstanceOf[Clinic]
  }
  
  def getMessageSource(servletContext : ServletContext): MessageSource = {
    getWebApplicationContext(servletContext).getBean("messageSource").asInstanceOf[MessageSource]
  }

}
